package com.shouer.doc.message;

import android.os.Parcel;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * Created by YYY-Work on 2017/8/17.
 */
@MessageTag(
            value = "App:txtMsg",
            flag = MessageTag.NONE
)
public class AppTxtMsgMessage extends MessageContent {

    private String content;
    protected String extra;

    public static AppTxtMsgMessage obtain(String content,String extra) {
        AppTxtMsgMessage model = new AppTxtMsgMessage();
        model.setContent(content);
        model.setExtra(extra);
        return model;
    }

    protected AppTxtMsgMessage() {
    }

    //给消息赋值。
    public AppTxtMsgMessage(Parcel in) {
        content= ParcelUtils.readFromParcel(in);
        extra= ParcelUtils.readFromParcel(in);
    }


    public AppTxtMsgMessage(byte[] data) {
        String jsonStr = null;

        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e1) {

        }

        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            if (jsonObj.has("content"))
            {
                content = jsonObj.optString("content");
            }
            if (jsonObj.has("extra"))
            {
                extra = jsonObj.optString("extra");
            }


        } catch (JSONException e) {
            RLog.e(AppTxtMsgMessage.class.getSimpleName(), "JSONException"+e.getMessage());
        }
    }

    /**
     * 读取接口，目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。
     */
    public static final Creator<AppTxtMsgMessage> CREATOR = new Creator<AppTxtMsgMessage>() {

        @Override
        public AppTxtMsgMessage createFromParcel(Parcel source) {
            return new AppTxtMsgMessage(source);
        }

        @Override
        public AppTxtMsgMessage[] newArray(int size) {
            return new AppTxtMsgMessage[size];
        }
    };

    @Override
    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();

        try {
            jsonObj.put("content", content);
            jsonObj.put("extra", extra);
        } catch (JSONException e) {
            Log.e("JSONException", e.getMessage());
        }
        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int i) {
        ParcelUtils.writeToParcel(dest, this.getContent());
        ParcelUtils.writeToParcel(dest, this.getExtra());
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }

}
